package com.ohuang.jiaxiaotong.Util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.ohuang.jiaxiaotong.R;

public class Ohuang {

    //修改手机状态栏字体图标颜色
    public static void changeStatusBarTextImgColor(Activity activity, boolean isBlack) {
        if (isBlack) {
            //设置状态栏黑色字体
            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            //恢复状态栏白色字体
            activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        }
    }


    //提示对话框
    public static void dialogshow(Context context, String a, String s) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_sign, null);
        final AlertDialog dialog = new AlertDialog.Builder(context, R.style.MyDialog).setView(view).create();
        TextView textView = view.findViewById(R.id.dialog_text_sign1);
        TextView textView1 = view.findViewById(R.id.dialog_text_sign0);
        textView1.setText(a);
        textView.setText(s);
        Button button = view.findViewById(R.id.dialog_btn_sign);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
        //需要先显示再设置大小
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();//获取屏幕分辨率
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        params.width = (int) (0.7 * screenWidth);
        window.setAttributes(params);
    }

    //提示对话框
    public static void dialogshow(Context context, String a, String s, View.OnClickListener onClickListener) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_sign, null);
        final AlertDialog dialog = new AlertDialog.Builder(context, R.style.MyDialog).setView(view).create();
        TextView textView = view.findViewById(R.id.dialog_text_sign1);
        TextView textView1 = view.findViewById(R.id.dialog_text_sign0);
        textView1.setText(a);
        textView.setText(s);
        Button button = view.findViewById(R.id.dialog_btn_sign);
        button.setOnClickListener(onClickListener);
        dialog.show();
        //需要先显示再设置大小
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();//获取屏幕分辨率
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        params.width = (int) (0.7 * screenWidth);
        window.setAttributes(params);
    }

    //再次确认对话框
    public static Dialog reconfirmDialogShow(Context context, String title, String content, View.OnClickListener confirmListener) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_exit, null);
        final Dialog dialog = new AlertDialog.Builder(context, R.style.MyDialog).setView(view).create();
        TextView text_title = view.findViewById(R.id.txt_title_dialog_exit);
        TextView text_content = view.findViewById(R.id.txt_content_dialog_exit);
        Button quxiao = view.findViewById(R.id.dialog_btn_exit_no);
        Button queding = view.findViewById(R.id.dialog_btn_exit_ok);
        text_title.setText(title);
        text_content.setText(content);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        queding.setOnClickListener(confirmListener);
        dialog.show();
        //需要先显示再设置大小
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();//获取屏幕分辨率
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        params.width = (int) (0.7 * screenWidth);
        window.setAttributes(params);
        return dialog;
    }

    //删除对话框
    public static Dialog DeleteDialogShow(Context context, String title, String content, View.OnClickListener confirmListener){
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_delete, null);
        final Dialog dialog = new AlertDialog.Builder(context, R.style.MyDialog).setView(view).create();
        TextView text_title = view.findViewById(R.id.txt_title_dialog_delete);
        TextView text_content = view.findViewById(R.id.txt_content_dialog_delete);
        Button quxiao = view.findViewById(R.id.dialog_btn_delete_no);
        Button queding = view.findViewById(R.id.dialog_btn_delete);
        text_title.setText(title);
        text_content.setText(content);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        queding.setOnClickListener(confirmListener);
        dialog.show();
        //需要先显示再设置大小
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();//获取屏幕分辨率
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        params.width = (int) (0.7 * screenWidth);
        window.setAttributes(params);
        return dialog;
    }



    //加载对话框
    public static Dialog dialogLoadingshow(Context context, String aa) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null);
        Dialog dialogLoading = new AlertDialog.Builder(context, R.style.LoadingDialog).setView(view).create();
        ImageView imageView = view.findViewById(R.id.dialog_img_loading);
        TextView textView = view.findViewById(R.id.dialog_text_loading);
        textView.setText(aa);
        Glide.with(context).load(R.drawable.load_0_gif).into(imageView);//使用Glide加载动图
        dialogLoading.show();
        Window window = dialogLoading.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = context.getResources().getDisplayMetrics();//获取屏幕分辨率
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        params.width = (int) (0.9 * screenWidth);
        params.y = (int) (0.05 * screenHeight);
        window.setAttributes(params);
        window.setGravity(Gravity.TOP);
        return dialogLoading;
    }

    //写入文件
    public static void writefile(Context context, String filename, String text) throws IOException {

        OutputStream out = context.openFileOutput(filename, Context.MODE_PRIVATE);//覆盖
        byte[] bytes = text.getBytes();
        out.write(bytes, 0, bytes.length);
        out.close();
    }

    //读取文件
    public static String readfile(Context context, String filename) throws IOException {
        InputStream in = context.openFileInput(filename);
        byte[] bytes = new byte[1024];
        StringBuffer sb = new StringBuffer();
        int len = -1;
        while ((len = in.read(bytes)) != -1) {
            sb.append(new String(bytes, 0, len));
        }
        in.close();
        return sb.toString();
    }
}
